﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonUtils;
using TERP.WaterClient.DbServices;
using TERP.WaterClient.Common;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmCustomerSearch : FrmMdiBase
    {
		CustomerService _customerService;

		public delegate void SendValue(string _strCusID, string _strAddress, string _strCusName, string _strCusTel, string _strOrgName, string _strDebit);
		public SendValue _sendValue = null;

        void InitGrid()
        {
            OrgName.FieldName = TblCustomer.OrgName;
			cusName.FieldName = TblCustomer.OrgName; ;
			CusAddress.FieldName = TblCustomer.Address; ;
			CusTel.FieldName = TblCustomer.CusTel; ;
			Note.FieldName = TblCustomer.Note; ;
			CusCode.FieldName = TblCustomer.CusCode;

            //gridView1.Editable = false;
        }

        void LoadData(string strCusCode,string strCusName)
        {
			_customerService = new CustomerService();
			DataSet dsCustomer = _customerService.Its_Customer_Get(""
																	, strCusCode
																, strCusName
																, ""
																, ""
																);

			gridControl1.DataSource = dsCustomer.Tables[DbTable.Tbl_Customer];
        }

        public FrmCustomerSearch()
        {
            InitializeComponent();

            InitGrid();
        }

        public FrmCustomerSearch(string strCusCode,string strCusName)
        {
            InitializeComponent();
            InitGrid();
			LoadData(strCusCode, strCusName);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmCustomerSearch_Load(object sender, EventArgs e)
        {
            //_dbService
            
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
			DataRow drow = gridView1.GetDataRow(gridView1.FocusedRowHandle);
			if (_sendValue != null)
			{
				_sendValue(
							drow[TblStockOut.CusID].ToString()
							, drow[TblCustomer.Address].ToString()
							, drow[TblCustomer.CusCode].ToString()
							, drow[TblCustomer.CusTel].ToString()
							, drow[TblCustomer.OrgName].ToString()
							, drow[TblCustomer.Debit].ToString()
							);
			}
			this.Close();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
			if (e.KeyCode == Keys.Enter)
			{
				DataRow drow = gridView1.GetDataRow(gridView1.FocusedRowHandle);
				if (_sendValue != null)
				{
					_sendValue(
								drow[TblStockOut.CusID].ToString()
								, drow[TblCustomer.Address].ToString()
								, drow[TblCustomer.CusCode].ToString()
								, drow[TblCustomer.CusTel].ToString()
								, drow[TblCustomer.OrgName].ToString()
								, drow[TblCustomer.Debit].ToString()
								);
				}
				this.Close();
			}
        }
    }
}
